package com.leoman.user.service.impl;

import com.leoman.common.service.impl.GenericManagerImpl;
import com.leoman.creator.dao.CreatorDao;
import com.leoman.creator.entity.Creator;
import com.leoman.user.dao.UserCheckDao;
import com.leoman.user.dao.UserInfoDao;
import com.leoman.user.entity.UserCheck;
import com.leoman.user.entity.UserInfo;
import com.leoman.user.service.UserCheckService;
import com.leoman.user.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户审核申请
 * Created by 涂奕恒 on 2016/6/14 0014.
 */
@Service
public class UserCheckServiceImpl extends GenericManagerImpl<UserCheck, UserCheckDao> implements UserCheckService {

    @Autowired
    private UserCheckDao userCheckDao;

    @Autowired
    private CreatorDao creatorDao;

    @Autowired
    private UserInfoDao userInfoDao;

    @Autowired
    private UserService userService;

    @Override
    public Page<UserCheck> page(final String userName, final String profession, final String mobile, final Integer status, final Long startDate, final Long endDate, Integer pageNum, Integer pageSize) {
        Pageable pageable = new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id");

        return userCheckDao.findAll(new Specification<UserCheck>() {
            @SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
            @Override
            public Predicate toPredicate(Root<UserCheck> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate result = null;
                List<Predicate> predicateList = new ArrayList<>();
                if (StringUtils.isNotBlank(userName)) {
                    Predicate pre = cb.like(root.get("userName").as(String.class), "%" + userName + "%");
                    predicateList.add(pre);
                }
                if (StringUtils.isNotBlank(profession)) {
                    Predicate pre = cb.like(root.get("profession").as(String.class), "%" + profession + "%");
                    predicateList.add(pre);
                }
                if (StringUtils.isNotBlank(mobile)) {
                    Predicate pre = cb.like(root.get("mobile").as(String.class), "%" + mobile + "%");
                    predicateList.add(pre);
                }
                if (null != status) {
                    Predicate pre = cb.equal(root.get("status").as(Integer.class), status);
                    predicateList.add(pre);
                }
                if (null != startDate) {
                    Predicate pre = cb.gt(root.get("createDate").as(Long.class), startDate);
                    predicateList.add(pre);
                }
                if (null != endDate) {
                    Predicate pre = cb.lt(root.get("createDate").as(Long.class), endDate);
                    predicateList.add(pre);
                }
                if (predicateList.size() > 0) {
                    result = cb.and(predicateList.toArray(new Predicate[]{}));
                }

                if (result != null) {
                    query.where(result);
                }
                return query.getGroupRestriction();
            }

        }, pageable);
    }

    @Override
    public UserCheck getById(Long id) {
        return userCheckDao.findOne(id);
    }

    @Override
    @Transient
    public Integer changeCheckStatus(Long id, Integer status) {
        try {
            UserCheck userCheck = userCheckDao.findOne(id);

            if (null != userCheck) {
                userCheck.setStatus(status);
                userCheck.setUpdateDate(System.currentTimeMillis());

                userCheckDao.save(userCheck);

                // 审核通过后增加主创关联信息
                if (status == 1) {
                    Creator creator = new Creator();
                    creator.setStatus(0);
                    creatorDao.save(creator);

                    UserInfo userInfo = userInfoDao.findOne(userCheck.getUserId());
                    userInfo.setIsCreator(3);
                    userInfo.setCreator(creator);
                    userInfoDao.save(userInfo);
                }
            }

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
